// ====================================================================================
// RibbonTable.cs
//
// Created by: NI, 2007.03.27
// Revised by: NI, 2007.11.23
// ====================================================================================
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Reflection;

namespace RibbonExplorer.Storage
{
  // ====================================================================================
  /// <summary>
  /// This class is represents a ribbon table that holds all the information about
  /// the controls on a specific ribbon.
  /// </summary>
  // ====================================================================================
  public class RibbonTable
  {
    #region Private fields

    private readonly string _Id;
    private readonly List<RibbonTableItem> _ControlList = new List<RibbonTableItem>();

    #endregion

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Initializes a ribbon table with the specified ID.
    /// </summary>
    /// <param name="id">ID of the ribbon table</param>
    // --------------------------------------------------------------------------------------------
    #region Lifecycle methods

    public RibbonTable(string id)
    {
      _Id = id;
    }

    #endregion

    #region Public propertiees

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Gets the ID of the ribbon table
    /// </summary>
    // --------------------------------------------------------------------------------------------
    public string Id
    {
      get { return _Id; }
    }

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Gets the List of items the ribbon table
    /// </summary>
    // --------------------------------------------------------------------------------------------
    public List<RibbonTableItem> ControlList
    {
      get { return _ControlList; }
    }

    #endregion

    #region Public methods

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Reads the content of the control table from resources embedded into this assembly.
    /// </summary>
    // --------------------------------------------------------------------------------------------
    public int ReadTable()
    {
      int itemCount = 0;

      // --- Obtain resource stream
      string resourceKey = String.Format("RibbonExplorer.RibbonTables.{0}.comp", _Id);
      Stream sourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceKey);

      // --- Decompress the resource files
      byte[] buffer = new byte[sourceStream.Length];
      int count = sourceStream.Read(buffer, 0, buffer.Length);
      if (count != buffer.Length)
      {
        sourceStream.Close();
        throw new InvalidOperationException("TUnable to read data from file");
      }
      sourceStream.Close();
      MemoryStream compressedMs = new MemoryStream(buffer);
      BinaryReader br = new BinaryReader(compressedMs);
      int originalLength = br.ReadInt32();
      byte[] outputBuffer = new byte[originalLength];
      GZipStream decompressedzipStream = new GZipStream(compressedMs, CompressionMode.Decompress);
      decompressedzipStream.Read(outputBuffer, 0, originalLength);
      decompressedzipStream.Close();
      MemoryStream resourceStream = new MemoryStream(outputBuffer);

      // --- Read in all lines
      using (TextReader reader = new StreamReader(resourceStream))
      {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
          // --- Create the ribbon item
          RibbonTableItem item = new RibbonTableItem(line);
          _ControlList.Add(item);
          itemCount++;
        }
      }
      return itemCount;
    }

    #endregion
  }
}
